package com.lvtu.monitor.module.web.ngxlog;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lvmama.com.lvtu.common.utils.DateUtil;
import com.lvtu.monitor.module.web.module.ResultHandler;
import com.lvtu.monitor.module.web.ngxlog.po.ApiAbnormalLogInfo;
import com.lvtu.monitor.module.web.ngxlog.po.ApiLogCountInfo;
import com.lvtu.monitor.utils.Constants;
import com.lvtu.monitor.utils.ElasticUtils;

/**
 * @Title: ApiLogQueryAction.java
 * @Package com.lvtu.monitor.module.web.ngxlog
 * @Description: 从索引库中查询nginx log的统计数据和异常记录
 * @author lvzimin
 * @date 2015年6月4日 上午10:41:59
 * @version V1.0.0
 */
@Controller
@RequestMapping("/apilog")
public class ApiLogQueryAction {

	/**
	 * 进入nginx日志统计列表
	 * 
	 * @return
	 */
	@RequestMapping("/toCount")
	public String toCount() {
		return "apilog/api_log_count";
	}

	/**
	 * 进入nginx错误日志列表
	 * 
	 * @return
	 */
	@RequestMapping("/toError")
	public String toError() {
		return "apilog/api_error_log";
	}

	/**
	 * 进入nginx访问慢日志列表
	 * 
	 * @return
	 */
	@RequestMapping("/toSlow")
	public String toSlow() {
		return "apilog/api_slow_log";
	}

	/**
	 * 加载nginx日志统计数据
	 * 
	 * @param date
	 *            日期
	 * @param method
	 *            方法
	 * @param page
	 *            页数
	 * @param rows
	 *            每页记录数
	 * @return
	 */
	@RequestMapping("/queryLogCount")
	@ResponseBody
	public ResultHandler queryLogCount(String date, String method, int page,
			int rows) {

		// 默认查询今天的统计数据
		if (StringUtils.isEmpty(date)) {
			date = DateUtil.formatDate(new Date(), "yyyyMMdd");
		} else {
			date = date.replaceAll("-", "");
		}
		BoolQueryBuilder query = QueryBuilders.boolQuery()
				.must(QueryBuilders.termQuery("date", date))
				.must(QueryBuilders.termQuery("_type", "ngx_log"));

		// 按method模糊查询
		if (StringUtils.isNotEmpty(method)) {
			query.must(QueryBuilders.termQuery("method", method));
		}

		// TODO count类型应改成数值
		SortBuilder sort = SortBuilders.fieldSort("count")
				.order(SortOrder.DESC);

		// 执行分页查询
		Long count = ElasticUtils.getInstance().countIndex(
				Constants.ELASTIC_INDEX_NGX_LOG, query, null);
		List<ApiLogCountInfo> logCountInfos = ElasticUtils.getInstance()
				.queryIndex(Constants.ELASTIC_INDEX_NGX_LOG,
						ApiLogCountInfo.class, query, null, sort,
						(page - 1) * rows, rows);
		ResultHandler result = new ResultHandler();
		result.setTotal(count.intValue());
		result.setRows(logCountInfos);
		return result;
	}

	/**
	 * 加载nginx状态码错误的日志
	 * 
	 * @param status
	 *            状态码
	 * @param method
	 *            方法
	 * @param page
	 *            页数
	 * @param rows
	 *            每页记录数
	 * @return
	 */
	@RequestMapping("/queryErrorLog")
	@ResponseBody
	public ResultHandler queryErrorLog(String status, String method, int page,
			int rows) {

		BoolQueryBuilder query = QueryBuilders.boolQuery().must(
				QueryBuilders.termQuery("_type", "error_log"));

		// 按status查询
		if (StringUtils.isNotEmpty(status)) {
			query.must(QueryBuilders.termQuery("status", status));
		}

		// 按method模糊查询
		// TODO api.com这种格式的查找不到
		if (StringUtils.isNotEmpty(method)) {
			query.must(QueryBuilders.termQuery("method", method));
		}

		SortBuilder sort = SortBuilders.fieldSort("status").order(SortOrder.DESC);

		// 执行分页查询
		Long count = ElasticUtils.getInstance().countIndex(
				Constants.ELASTIC_INDEX_NGX_LOG, query, null);
		List<ApiAbnormalLogInfo> logInfos = ElasticUtils.getInstance()
				.queryIndex(Constants.ELASTIC_INDEX_NGX_LOG,
						ApiAbnormalLogInfo.class, query, null, sort,
						(page - 1) * rows, rows);
		ResultHandler result = new ResultHandler();
		result.setTotal(count.intValue());
		result.setRows(logInfos);
		return result;
	}
	
	/**
	 * 加载nginx访问缓慢的日志
	 * 
	 * @param status
	 *            状态码
	 * @param method
	 *            方法
	 * @param page
	 *            页数
	 * @param rows
	 *            每页记录数
	 * @return
	 */
	@RequestMapping("/querySlowLog")
	@ResponseBody
	public ResultHandler querySlowLog(String status, String method, int page,
			int rows) {

		BoolQueryBuilder query = QueryBuilders.boolQuery().must(
				QueryBuilders.termQuery("_type", "slow_log"));

		// 按status查询
		if (StringUtils.isNotEmpty(status)) {
			query.must(QueryBuilders.termQuery("status", status));
		}

		// 按method模糊查询
		// TODO api.com这种格式的查找不到
		if (StringUtils.isNotEmpty(method)) {
			query.must(QueryBuilders.termQuery("method", method));
		}

		SortBuilder sort = SortBuilders.fieldSort("cost").order(SortOrder.DESC);

		// 执行分页查询
		Long count = ElasticUtils.getInstance().countIndex(
				Constants.ELASTIC_INDEX_NGX_LOG, query, null);
		List<ApiAbnormalLogInfo> logInfos = ElasticUtils.getInstance()
				.queryIndex(Constants.ELASTIC_INDEX_NGX_LOG,
						ApiAbnormalLogInfo.class, query, null, sort,
						(page - 1) * rows, rows);
		ResultHandler result = new ResultHandler();
		result.setTotal(count.intValue());
		result.setRows(logInfos);
		return result;
	}
}
